package com.wangsir.util;

import com.alibaba.fastjson.JSON;

public class ApiResult {
    //返回的信息
    private String msg;
    //返回成功还是失败
    private Boolean success;

    private Object data;
    //构造方法
    public ApiResult() {
    }
    //单元测试
    public static void main(String[] args) {
        //返回到前端后浏览器会解析为json格式的字符串！
        System.out.println(json(false,"删除失败！"));
    }
    //全参数的构造
    public ApiResult(String msg, Boolean success, Object data) {
        this.msg = msg;
        this.success = success;
        this.data = data;
    }
    //获取json对象
    public static String json(Boolean success,String msg,  Object data){
        ApiResult apiResult = new ApiResult();
        apiResult.setSuccess(success);
        apiResult.setMsg(msg);
        apiResult.setData(data);
        System.out.println("原本的java对象");
        System.out.println(apiResult);
        //将该类的对象转换为：JSON格式字符串
        System.out.println("利用阿里的fastjson转换为json格式的的字符串，因此前端人员才可以看的懂");
        String jsonString = JSON.toJSONString(apiResult);
        return jsonString;
    }

    public static String json(Boolean success,String msg){
        return json(success,msg,null);
    }

    /**
     * 获取
     * @return msg
     */
    public String getMsg() {
        return msg;
    }

    /**
     * 设置
     * @param msg
     */
    public void setMsg(String msg) {
        this.msg = msg;
    }

    /**
     * 获取
     * @return success
     */
    public boolean isSuccess() {
        return success;
    }

    /**
     * 设置
     * @param success
     */
    public void setSuccess(boolean success) {
        this.success = success;
    }

    /**
     * 获取
     * @return data
     */
    public Object getData() {
        return data;
    }

    /**
     * 设置
     * @param data
     */
    public void setData(Object data) {
        this.data = data;
    }

    public String toString() {
        return "ApiResult{msg = " + msg + ", success = " + success + ", data = " + data + "}";
    }
}
